from tkinter import *
from page1 import *
from page2 import *
import json
# from PIL import Image
# from PIL import ImageTk


# 不使用驼峰，类实例小写连写，其他下划线分隔小写

# class G:
#     img = None  # canvas图片保持不被回收


# tk事件的字母键，只能用小写字母
# B1-Motion的语义并不是“在其上按住B1移动”，而是“由其上发起的，按住B1的移动”。和leave-enter一个德行




class VisualLamp(Tk):
    MODE_KNOWLEDGE = 1
    MODE_TIME = 2
    MODE_TABLE = 3
    def __init__(self):
        super().__init__()
        self.iconbitmap("lamp.ico")
        self.title("vl")
        self.geometry("900x600")
        #self.wm_state('zoomed')
        self.mode_dict = {1:Page1(self), 2:Page2(self)}
        self.mode = self.mode_dict[1]
        self.mode_var = IntVar(value=self.mode)
        self.mode_var.set(1)
        def _(*a):
            mode = self.mode_dict[self.mode_var.get()]
            if mode != self.mode:
                self.mode.end()
                mode.start()
                self.mode = mode
        self.mode_var.trace('w', _)
        self.create_menu()
        self.config_global_event()
        self.mode.start()
        
        


    def create_menu(self):
        menu = Menu(self)
        menu_file = Menu(menu, tearoff=0)
        menu_file.add_command(label=" 导入 ", command=self.load, image=None, compound='left')
        menu_file.add_command(label=" 导出 ", command=self.save, image=None, compound='left')
        menu_file.add_command(label=" 清空 ", command=self.clear, image=None, compound='left')
        menu.add_cascade(label=" 文件 ", menu=menu_file)
        menu_view = Menu(menu, tearoff=0)
        menu_view.add_radiobutton(label=" 知识 ", variable=self.mode_var, value=self.MODE_KNOWLEDGE, image=None, compound='left')
        menu_view.add_radiobutton(label=" 时间 ", variable=self.mode_var, value=self.MODE_TIME, image=None, compound='left')
        menu_view.add_radiobutton(label=" 表格 ", variable=self.mode_var, value=self.MODE_TABLE, image=None, compound='left')
        menu.add_cascade(label=" 视图 ", menu=menu_view)
        self.config(menu=menu)


    def config_global_event(self):
        self.eventer = Eventer(b_key=self.bind)
        eventer = self.eventer
        @eventer.second_key_bind("<a>", None) 
        def _(e): eventer.change_state2("a_pressed")
        @eventer.second_key_bind("<KeyRelease-a>", "a_pressed")
        def _(e): eventer.change_state2(None)
        # @eventer.second_key_bind("<t>", None) 
        # def _(e): eventer.change_state2("t_pressed")
        # @eventer.second_key_bind("<KeyRelease-t>", "t_pressed")
        # def _(e): eventer.change_state2(None)
        @eventer.second_key_bind("<Delete>", None)
        def _(e): eventer.change_state2("del_pressed")
        @eventer.second_key_bind("<KeyRelease-Delete>", "del_pressed")
        def _(e): eventer.change_state2(None)
        @eventer.second_key_bind("<f>", None)
        def _(e): eventer.change_state2("f_pressed")
        @eventer.second_key_bind("<KeyRelease-f>", "f_pressed")
        def _(e):eventer.change_state2(None)   # 防组合键失手
        @eventer.second_key_bind("<g>", None)
        def _(e): eventer.change_state2("g_pressed")
        @eventer.second_key_bind("<KeyRelease-g>", "g_pressed")
        def _(e): eventer.change_state2(None)  
        @eventer.second_key_bind("<v>", None)
        def _(e): eventer.change_state2("v_pressed")
        @eventer.second_key_bind("<KeyRelease-v>", "v_pressed")
        def _(e): eventer.change_state2(None).change_state("nothing")


    def save(self):
        with open("info.txt", 'w') as f:
            f.write(json.dumps(self.mode_dict[1].netshow.output()))

    def load(self):
        print("应当先清空再导入，或者合并")
        with open("info.txt", 'r') as f:
            self.mode_dict[1].netshow.input(json.loads(f.read()))

    def clear(self):
        print("没有实现清空功能")

visuallamp = VisualLamp()

if not "test":
    e = lambda:None
    e.type, e.keysym = "fake_event", "a"
    e.x, e.y = 30, 20
    visuallamp.netshow.eventer.d["<a>"].call(e)
    visuallamp.netshow.eventer.d["<ButtonRelease-1>"].call(e)
    visuallamp.netshow.eventer.d["<KeyRelease-a>"].call(e)
    lampshape = visuallamp.netshow.findlampshape(e.x, e.y)
    lampshape.set_text("TEST")
    lampshape.gray()
    visuallamp.netshow.netcontrol.add_lamp(lampshape.rectid)
    e.x, e.y = 90, 100
    visuallamp.netshow.eventer.d["<a>"].call(e)
    visuallamp.netshow.eventer.d["<ButtonRelease-1>"].call(e)
    visuallamp.netshow.eventer.d["<KeyRelease-a>"].call(e)
    lampshape = visuallamp.netshow.findlampshape(e.x, e.y)
    lampshape.set_text("TEST2")
    lampshape.gray()
    visuallamp.netshow.netcontrol.add_lamp(lampshape.rectid)


visuallamp.mainloop()